/*
Unicode Encoding    | UTF-8  Encoding
     Hexadecimal    |     Binary 
--------------------+--------------------------------------------- 
0000 0000-0000 007F | 0xxxxxxx 
0000 0080-0000 07FF | 110xxxxx 10xxxxxx 
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx 
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
View Chinese character UTF8 encoding URL：http://www.mytju.com/classcode/tools/encode_utf8.asp
Chinese character Unicode encoding URL：https://www.qqxiuzi.cn/zh/hanzi-unicode-bianma.php
*/

#include <stdio.h>
#include "stdlib.h"
#include "string.h"

int isValidUtf8(unsigned char * utf8Str,int *len);
int validUtf8String(unsigned char *srcUtf8,unsigned char *validUtf8);
int main()
{
    
    unsigned char *test="严格1233sdd";
    unsigned char validstr[128];

    if(0==validUtf8String(test,validstr)){
        printf("valid utf8str:%s\n",validstr);
    }else{
        printf("trans validutf8str fail\n");    
    }
    return 0;
}

int validUtf8String(unsigned char *srcUtf8,unsigned char *validUtf8){
    if(srcUtf8 == NULL || validUtf8==NULL){
        return -1;
    }else{
        int srclen=strlen(srcUtf8);
        int i,lenTmp;
        i=0;
        char *srcTmp =srcUtf8;
        while(i<srclen){
            if(isValidUtf8(srcTmp,&lenTmp)){
                char *strtmp = (char*)malloc(sizeof(char)*(lenTmp+1));
                memset(strtmp,0,lenTmp+1);
                strncpy(strtmp,srcTmp,lenTmp);
                strcat(validUtf8,strtmp);
                free(strtmp);     
            }
            srcTmp +=lenTmp;
            i+=lenTmp;
        }
        return 0;
    }
}

int isValidUtf8(unsigned char * utf8Str,int *len){
    int i=0;
    unsigned int uni=0;
    unsigned char x;
    if(utf8Str[0] <=0x7f){/////1 bytes represent a utf8 character
        //0000 0000-0000 007F | 0xxxxxxx 
        (*len) = 1;
         return 1;                              
    }else if(utf8Str[0] >= 0xc0 && utf8Str[0] <= 0xdf){/////2 bytes represent a utf8 character
        //0000 0080-0000 07FF | 110xxxxx 10xxxxxx 
        uni = (0x1f &utf8Str[0]);
        uni = uni<<6;
        x = (0x3f &utf8Str[1]);
        x = x<<2;
        uni |= x;
        (*len)=2;
        if(uni >=0x80  && uni <0x800 ){
              return 1;  
        }else
        {
            return 0;
        }
    }else if(utf8Str[0] >= 0xe0 && utf8Str[0] <= 0xef){/////3 bytes represent a utf8 character
        //0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
        uni = (0x0f &utf8Str[0]);
        uni = uni<<8;
        
        x= (0x3f &utf8Str[1]);
        x= x<<2;
        uni |= (x & 0xfc);
        uni= uni<<6;
        
        x= (0x3f &utf8Str[2]);
        x=x<<2;
        uni |= x;
        uni = uni>>2;
        (*len)=3;
        if(uni >=0x800  && uni <0x10000 ){
              return 1;  
        }else
        {
            return 0;
        }
    }else if(utf8Str[0] >= 0xf0 && utf8Str[0] <= 0xf3){/////4 bytes represent a utf8 character
        //0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
        uni = (0x07 &utf8Str[0]);
        uni = uni << 6;
        
        x = (0x3f &utf8Str[1]);
        x = x<<2;
        uni |= x;
        uni = uni<<6;
        
        x= (0x3f &utf8Str[2]);
        x = x << 2;
        uni |= x;
        uni = uni<<6;

        x= (0x3f &utf8Str[2]);
        x = x << 2;
        uni |= x;
        (*len)=4;
        if(uni >=0x10000  && uni <0x10ffff ){
              return 1;  
        }else
        {
            return 0;
        }
    }
}